<?php
	require_once('./src/classes/DBConnection.php');
	require('./src/config.php');
	require_once('./src/classes/LocalUser.php');
	
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
	header("Cache-Control: no-store, no-cache, must-revalidate");
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Pragma: no-cache");
	
	$connection = new DBConnection();
	$connection->connect($host, $username, $password, $dbname);
	
	if (! $connection->is_connected())
	{
		die($connection->get_error());
	}
	
	$curUser = new LocalUser($connection);
	$curUser->update_last_active($connection);
	
	if (! $curUser->isOnline || ! $curUser->has_rank("Admin", $connection))
	{
		die("You must be logged in as an administrator to edit the database.");
	}
	
	$name =				$connection->clean_var($_GET['name']);
	
	if ($name == "")
		die("You must enter a name to create the category.");
	
	$row = $connection->get_result_array("SELECT * FROM category WHERE name='$name'");
	
	if (!empty($row) || $name == "General")
	{
		die("A category by this name already exists.");
	}
	
	$query = "INSERT INTO category SET 
				name='$name'
	";
	
	if ($connection->query_noresult($query) == 0)
	{
		die($connection->get_error());
	}
	
	echo "Category created successfully!";
	
	$connection->close();
?>